package com.tools;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.login.R;

import java.util.Objects;


public class PayDialog extends Dialog {
    private ImageView cancel;
    private Button payBtn;
    private TextView paySecMoney;
    private EditText payGoodsMoney;
    private PayInputView inputView;
    private Context mContext;

    private String moneyText;

    private boolean isGoodsVis = false;
    private boolean isInputVis = true;

    public PayDialog(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_dialog);
        setCanceledOnTouchOutside(false);
        initView();
        refreshView();
    }

    @Override
    public void show() {
        super.show();
        refreshView();
    }


    private void initView(){
        inputView = findViewById(R.id.pay_dialog_pwd);
        paySecMoney = findViewById(R.id.pay_dialog_seckill);
        payGoodsMoney = findViewById(R.id.pay_dialog_goods);

        cancel = findViewById(R.id.pay_dialog_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCancelClickListener != null){
                    onCancelClickListener.onCancelClick();
                }
            }
        });

        payBtn = findViewById(R.id.pay_dialog_pay);
        payBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onPayClickListener != null){
                    onPayClickListener.onPayClick();
                }
            }
        });
    }

    private void refreshView(){
        if (!TextUtils.isEmpty(moneyText)) {
            paySecMoney.setText(moneyText);
        }else {
            paySecMoney.setVisibility(View.GONE);
        }

        if (isGoodsVis){
            payGoodsMoney.setVisibility(View.VISIBLE);
            paySecMoney.setVisibility(View.GONE);
        }else {
            payGoodsMoney.setVisibility(View.GONE);
            paySecMoney.setVisibility(View.VISIBLE);
        }

        if (isInputVis) inputView.setVisibility(View.VISIBLE);
        else inputView.setVisibility(View.GONE);
    }

    /**
     * get set方法
     */
    //得到输入的金额（GoodsDetail界面）
    public String getGoodsMoney(){
        return payGoodsMoney.getText().toString();
    }

    //得到输入的密码（两个都适用）
    public String getInputText(){
        return Objects.requireNonNull(inputView.getText()).toString();
    }

    public void setSecMoney(String text){
        moneyText = text;
    }

    public void setGoodsVis(boolean vis){
        isGoodsVis = vis;
    }

    public void setInputVis(boolean vis){
        isInputVis = vis;
    }

    /**
     * 点击接口
     */
    public OnPayClickListener onPayClickListener;
    public void setOnPayClickListener(OnPayClickListener onPayClickListener) {
        this.onPayClickListener = onPayClickListener;
    }
    public interface OnPayClickListener{
        void onPayClick();
    }

    public OnCancelClickListener onCancelClickListener;
    public void setOnCancelClickListener(OnCancelClickListener onCancelClickListener) {
        this.onCancelClickListener = onCancelClickListener;
    }
    public interface OnCancelClickListener{
        void onCancelClick();
    }
}
